package com.achao.service.impl;

import com.achao.dao.IdentificationDao;
import com.achao.dao.PassengerDao;
import com.achao.dao.TravellerDao;
import com.achao.dao.UserDao;
import com.achao.service.PassengerService;
import com.achao.utils.*;
import com.achao.view.UserRegView;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.util.List;

/**
 * @Author: achao
 * @DateTime: 2022/7/24 9:50
 * @Description: TODO
 */
@Log4j2
public class PassengerServiceImpl implements PassengerService {


    @Override
    public void frontRegister() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        TravellerDao travellerDao = sqlSession.getMapper(TravellerDao.class);
        IdentificationDao identificationDao = sqlSession.getMapper(IdentificationDao.class);
        PassengerDao passengerDao = sqlSession.getMapper(PassengerDao.class);


        UserRegView instance = UserRegView.getInstance();
        String userName = instance.getUsernameText().getText();
        String pwdText1 = instance.getPasswordText().getText();
        String pwdText2 = instance.getPassword2Text().getText();
        String idTypeName = null;
        String name = instance.getNameText().getText();
        String idNum = instance.getIdentificationNumText().getText();
        String phone = instance.getPhoneText().getText();
        String email = instance.getEmailText().getText();
        String tType = (String) instance.getTravellerType().getSelectedItem();
        List<JRadioButton> identificationTypeList = instance.getIdentificationTypeList();
        for (JRadioButton jRadioButton : identificationTypeList) {
            if (jRadioButton.isSelected()) {
                idTypeName = jRadioButton.getText();
            }
        }
        if (StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(pwdText1) && StringUtils.isNotEmpty(pwdText2) &&
                StringUtils.isNotEmpty(phone) && StringUtils.isNotEmpty(email) && StringUtils.isNotEmpty(idNum)) {

            // 获取外联ID
            int tTypeId = travellerDao.getIdByType(tType);
            int idTypeId = identificationDao.getIdByType(idTypeName);
            // 校验手机/邮箱/证件
            boolean phoneFlag = false;
            boolean emailFlag = false;
            boolean idNumFlag = false;
            if (CheckUtils.isPhone(phone))
                phoneFlag = true;
            else
                JOptionPane.showMessageDialog(instance, "手机格式有误!");
            if (CheckUtils.checkEmail(email))
                emailFlag = true;
            else
                JOptionPane.showMessageDialog(instance, "邮箱格式有误!");
            if (CheckUtils.checkIdNumber(idNum))
                idNumFlag = true;
            else
                JOptionPane.showMessageDialog(instance, "证件有误!");
            // 检验账号可用
            if (phoneFlag && emailFlag && idNumFlag) {
                int idByUserName = userDao.getIdByUserName(userName);
                if (idByUserName == 0) {
                    if (pwdText2.equals(pwdText1)) {
                        // 添加用户
                        String pwd = MD5Utils.md5(pwdText1 + ParamUtils.SECRET);
                        int add = userDao.add(userName, pwd);
                        if (add > 0) {
                            int userId = userDao.getIdByUserName(userName);
                            int addP = passengerDao.add(userId, idTypeId, name, idNum, phone, email, tTypeId);
                            if (addP > 0) {
                                JOptionPane.showMessageDialog(instance, "跳转到主页");
                            } else {
                                log.error("乘客添加失败!");
                            }
                        } else {
                            log.error("用户添加失败!");

                        }
                    }
                } else {
                    JOptionPane.showMessageDialog(instance, "此账号已存在!");
                }
            }
        } else {
            JOptionPane.showMessageDialog(instance, "请填写完整信息!");
        }
    }
}
